/*
 * Project Name: jmax
 * File Name: TransInit.java
 * Copyright: Copyright(C) 1985-2015 ZKTeco Inc. All rights reserved.
 */
package com.xdtech.trans.init;

import java.util.List;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xdtech.common.service.impl.BaseService;
import com.xdtech.common.utils.ApplicationContextUtil;
import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.core.init.SysInitOperation;
import com.xdtech.core.model.BaseModel;
import com.xdtech.sys.model.CodeValue;
import com.xdtech.sys.model.DictionaryCode;
import com.xdtech.sys.model.MenuFunction;
import com.xdtech.sys.model.Operation;
import com.xdtech.sys.model.Params;
import com.xdtech.sys.util.SysConstants;
import com.xdtech.trans.util.TransConstants;

/**
 * TODO 一句话功能简述，请确保和下面的block tags之间保留一行空行
 * <p>
 * TODO 功能详细描述，若不需要请连同上面的p标签一起删除
 * 
 * @author <a href="max.zheng@zkteco.com">郑志雄</>
 * @version TODO 添加版本
 * @see 相关类或方法，不需要请删除此行
 * @since 2015-5-27 下午12:48:45
 */
public class TransInit implements SysInitOperation{
	Log log = LogFactory.getLog(TransInit.class);

	/* (非 Javadoc)
	 * initingToDb
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param baseService
	 * @see com.xdtech.core.init.SysInitOperation#initingToDb(com.xdtech.common.service.impl.BaseService)
	 */
	@Override
	public void initingToDb(BaseService<BaseModel> baseService)
	{
		List params = (List) baseService.findByHql("from Params where code=? and codeValue=?", "TransInit","true");
		log.info("快件模块数据库初始化......");
		if (EmptyUtil.isEmpty(params)) {
			initData(baseService);
			Params p = new Params("TransInit", "true", "快件模块数据库初始化标记");
			baseService.save(p);
			log.info("快件模块数据库初始化......结束");
		}else {
			log.info("快件模块数据库已经初始化......完毕");
		}
		
	}

	/**
	 * 
	 * @author max.zheng
	 * @create 2015-5-31下午11:08:33
	 * @modified by
	 * @param baseService
	 */
	private void initData(BaseService<BaseModel> baseService) {
		MenuFunction m1 = new MenuFunction();
		m1.setIconName("icon-save");
		m1.setNameCN("快递管理");
		m1.setOperationCode("trans-web-manage");
		baseService.save(m1);
		
		MenuFunction m11 = new MenuFunction();
		m11.setIconName("icon-save");
		m11.setNameCN("网点管理");
		m11.setOperationCode("website-manage");
		m11.setPageUrl("transWebSite.do?transWebSite");
		m11.setParent(m1);
		baseService.save(m11);
		
		createButtonOperations("新增","sys:website:add",m11,baseService);
		createButtonOperations("删除","sys:website:delete",m11,baseService);
		createButtonOperations("更新或保存","sys:website:saveOrUpdate",m11,baseService);
		
		MenuFunction m12 = new MenuFunction();
		m12.setIconName("icon-save");
		m12.setNameCN("员工管理");
		m12.setOperationCode("person-manage");
		m12.setParent(m1);
		m12.setPageUrl("person.do?person");
		baseService.save(m12);
		createButtonOperations("新增","trans:person:add",m12,baseService);
		createButtonOperations("删除","trans:person:delete",m12,baseService);
		createButtonOperations("编辑","trans:person:edit",m12,baseService);
		createButtonOperations("刷新","trans:person:refresh",m12,baseService);
		
		MenuFunction m13 = new MenuFunction();
		m13.setIconName("icon-save");
		m13.setNameCN("账户管理");
		m13.setOperationCode("account-manage");
		m13.setParent(m1);
		m13.setPageUrl("transAccount.do?transAccount");
		baseService.save(m13);
		createButtonOperations("账户设置","trans:transAccount:set",m13,baseService);
		createButtonOperations("资金入账","trans:transAccount:in",m13,baseService);
		createButtonOperations("资金提取","trans:transAccount:out",m13,baseService);
		createButtonOperations("交易记录","trans:transAccount:tradeRecord",m13,baseService);
		createButtonOperations("刷新","trans:transAccount:refresh",m13,baseService);
		
		MenuFunction m14 = new MenuFunction();
		m14.setIconName("icon-save");
		m14.setNameCN("运单管理");
		m14.setOperationCode("transorder-manage");
		m14.setParent(m1);
		m14.setPageUrl("transOrder.do?transOrder");
		baseService.save(m14);
		createButtonOperations("新增","trans:transOrder:add",m14,baseService);
		createButtonOperations("删除","trans:transOrder:delete",m14,baseService);
		createButtonOperations("编辑","trans:transOrder:edit",m14,baseService);
		createButtonOperations("审核通过","trans:transOrder:check",m14,baseService);
		createButtonOperations("取消审核","trans:transOrder:cancleCheck",m14,baseService);
		createButtonOperations("刷新","trans:transOrder:refresh",m14,baseService);
		
		MenuFunction m15 = new MenuFunction();
		m15.setIconName("icon-save");
		m15.setNameCN("寄件扫描");
		m15.setOperationCode("start-scan-manage");
		m15.setParent(m1);
		m15.setPageUrl("transOrderScan.do?page=start");
		baseService.save(m15);
		createButtonOperations("新增","trans:startOrderScan:add",m15,baseService);
		createButtonOperations("删除","trans:startOrderScan:delete",m15,baseService);
		createButtonOperations("更新或保存","trans:startOrderScan:saveOrUpdate",m15,baseService);
		
		MenuFunction m16 = new MenuFunction();
		m16.setIconName("icon-save");
		m16.setNameCN("发件扫描");
		m16.setOperationCode("send-scan-manage");
		m16.setParent(m1);
		m16.setPageUrl("transOrderScan.do?page=send");
		baseService.save(m16);
		createButtonOperations("新增","trans:sendOrderScan:add",m16,baseService);
		createButtonOperations("删除","trans:sendOrderScan:delete",m16,baseService);
		createButtonOperations("更新或保存","trans:sendOrderScan:saveOrUpdate",m16,baseService);
		
		MenuFunction m17 = new MenuFunction();
		m17.setIconName("icon-save");
		m17.setNameCN("到件扫描");
		m17.setOperationCode("arrive-scan-manage");
		m17.setParent(m1);
		m17.setPageUrl("transOrderScan.do?page=arrive");
		baseService.save(m17);
		createButtonOperations("新增","trans:arriveOrderScan:add",m17,baseService);
		createButtonOperations("删除","trans:arriveOrderScan:delete",m17,baseService);
		createButtonOperations("更新或保存","trans:arriveOrderScan:saveOrUpdate",m17,baseService);
		
		MenuFunction m18 = new MenuFunction();
		m18.setIconName("icon-save");
		m18.setNameCN("派件扫描");
		m18.setOperationCode("delivery-scan-manage");
		m18.setParent(m1);
		m18.setPageUrl("transOrderScan.do?page=delivery");
		baseService.save(m18);
		createButtonOperations("新增","trans:deliveryOrderScan:add",m18,baseService);
		createButtonOperations("删除","trans:deliveryOrderScan:delete",m18,baseService);
		createButtonOperations("更新或保存","trans:deliveryOrderScan:saveOrUpdate",m18,baseService);
		
		MenuFunction m19 = new MenuFunction();
		m19.setIconName("icon-save");
		m19.setNameCN("签收录入");
		m19.setOperationCode("sign-in-manage");
		m19.setParent(m1);
		m19.setPageUrl("transOrderScan.do?page=sign");
		baseService.save(m19);
		createButtonOperations("新增","trans:signOrderScan:add",m19,baseService);
		createButtonOperations("删除","trans:signOrderScan:delete",m19,baseService);
		createButtonOperations("更新或保存","trans:signOrderScan:saveOrUpdate",m19,baseService);
		
		MenuFunction m110 = new MenuFunction();
		m110.setIconName("icon-save");
		m110.setNameCN("快件跟踪");
		m110.setOperationCode("transorder-tracking-manage");
		m110.setParent(m1);
		m110.setPageUrl("transOrder.do?page=transOrderTracking");
		baseService.save(m110);
		createButtonOperations("查询","trans:transOrderTrack:search",m110,baseService);
		createButtonOperations("清除","trans:transOrderTrack:clear",m110,baseService);
		
		MenuFunction m111 = new MenuFunction();
		m111.setIconName("icon-save");
		m111.setNameCN("价格设置");
		m111.setOperationCode("price-config-manage");
		m111.setParent(m1);
		m111.setPageUrl("priceConfig.do?setPriceConfig");
		baseService.save(m111);
		createButtonOperations("更新或保存","trans:priceConfig:save",m111,baseService);
		
		//数据字典
		DictionaryCode dictionaryCode = new DictionaryCode("ACCOUNT_TYPE","账户类型");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue("1","预付款账户",1,dictionaryCode));
//		baseService.save(new CodeValue("2","一般账户",2,dictionaryCode));
//		baseService.save(new CodeValue("3","现金账户",3,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("WEBSITE_TYPE","网点类型");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue("1","自营",1,dictionaryCode));
		baseService.save(new CodeValue("2","加盟",2,dictionaryCode));
		baseService.save(new CodeValue("3","控股",3,dictionaryCode));
		baseService.save(new CodeValue("4","代理",4,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("WEBSITE_LEVEL","网点级别");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue("1","分拨中心",1,dictionaryCode));
		baseService.save(new CodeValue("2","一级",2,dictionaryCode));
		baseService.save(new CodeValue("3","二级",3,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("POSITION_STATUS","任职状态");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue("1","在职",1,dictionaryCode));
		baseService.save(new CodeValue("2","离职",2,dictionaryCode));
		baseService.save(new CodeValue("3","停薪留职",3,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("MARITAL_STATUS","婚姻状况");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue("1","未婚",1,dictionaryCode));
		baseService.save(new CodeValue("2","已婚",2,dictionaryCode));
		baseService.save(new CodeValue("3","离异",3,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("EDUCATION_LEVEL","文化程度");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue("1","未知",1,dictionaryCode));
		baseService.save(new CodeValue("2","初中",2,dictionaryCode));
		baseService.save(new CodeValue("3","高中",3,dictionaryCode));
		baseService.save(new CodeValue("4","中专",4,dictionaryCode));
		baseService.save(new CodeValue("5","专科",5,dictionaryCode));
		baseService.save(new CodeValue("6","本科",6,dictionaryCode));
		baseService.save(new CodeValue("7","硕士",7,dictionaryCode));
		baseService.save(new CodeValue("8","博士",8,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("PERSON_TYPE","人员类型");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue("1","收件员",1,dictionaryCode));
		baseService.save(new CodeValue("2","派件员",2,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("TRADE_TYPE","交易类型");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue("in","资金入账",1,dictionaryCode));
		baseService.save(new CodeValue("out","资金提取",2,dictionaryCode));
		baseService.save(new CodeValue("send","寄件",3,dictionaryCode));
		baseService.save(new CodeValue("arrive","到付",4,dictionaryCode));
		baseService.save(new CodeValue("transChange","中转",5,dictionaryCode));
		baseService.save(new CodeValue("delivery","派件",6,dictionaryCode));
		baseService.save(new CodeValue("return","返还",7,dictionaryCode));
		
		
		
		dictionaryCode = new DictionaryCode("TRACK_TYPE","跟踪类型");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue("1","揽货成功",1,dictionaryCode));
		baseService.save(new CodeValue("2","发件成功",2,dictionaryCode));
		baseService.save(new CodeValue("3","到达成功",3,dictionaryCode));
		baseService.save(new CodeValue("4","派件成功",4,dictionaryCode));
		baseService.save(new CodeValue("5","签收成功",5,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("SIGN_STATUS","签收状态");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue(TransConstants.SIGN_STATUS_SIGN,"签收",1,dictionaryCode));
		baseService.save(new CodeValue(TransConstants.SIGN_STATUS_REJECTION,"退回",2,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("ORDER_STATUS","运单状态");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue(TransConstants.ORDER_STATUS_NEW,"新增",1,dictionaryCode));
		baseService.save(new CodeValue(TransConstants.ORDER_STATUS_OK,"审核通过",2,dictionaryCode));
		baseService.save(new CodeValue(TransConstants.ORDER_STATUS_NO,"审核失败",3,dictionaryCode));
		baseService.save(new CodeValue(TransConstants.ORDER_STATUS_WAITE_SEND,"待发",4,dictionaryCode));
		baseService.save(new CodeValue(TransConstants.ORDER_STATUS_SEND,"发件中",5,dictionaryCode));
		baseService.save(new CodeValue(TransConstants.ORDER_STATUS_DELIVERY,"派件中",6,dictionaryCode));
		baseService.save(new CodeValue(TransConstants.ORDER_STATUS_RETURN,"回单",7,dictionaryCode));
//		新增、审核通过、待发、已发、已派、回单
		baseService.save(new Params("START_SCAN_AUTO_SAVE", "Y", "寄件扫描是否自动保存",SysConstants.YES));
		baseService.save(new Params("SEND_SCAN_AUTO_SAVE", "Y", "发件扫描是否自动保存",SysConstants.YES));
		baseService.save(new Params("ARRIVE_SCAN_AUTO_SAVE", "Y", "到件扫描是否自动保存",SysConstants.YES));
		baseService.save(new Params("DELIVERY_SCAN_AUTO_SAVE", "Y", "派件扫描是否自动保存",SysConstants.YES));
		baseService.save(new Params("SIGN_SCAN_AUTO_SAVE", "Y", "签收录入是否自动保存",SysConstants.YES));
	}

	
	/**
	 * 
	 * @author max.zheng
	 * @create 2015-2-15下午10:29:11
	 * @modified by
	 * @param m11
	 */
	private static void createButtonOperations(String nameCN, String operationCode,
			MenuFunction menuFunction,BaseService baseService) {
		Operation operation = new Operation(nameCN, operationCode, menuFunction);
		baseService.save(operation); 
		
	}
	/* (非 Javadoc)
	 * initingToCache
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param baseService
	 * @see com.xdtech.core.init.SysInitOperation#initingToCache(com.xdtech.common.service.impl.BaseService)
	 */
	@Override
	public void initingToCache(BaseService<BaseModel> baseService)
	{
		
	}

	/* (非 Javadoc)
	 * getInitOrder
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @return
	 * @see com.xdtech.core.init.SysInitOperation#getInitOrder()
	 */
	@Override
	public int getInitOrder()
	{
		
		return 23;
	}

	/* (非 Javadoc)
	 * initingToWebApplication
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param baseService
	 * @see com.xdtech.core.init.SysInitOperation#initingToWebApplication(com.xdtech.common.service.impl.BaseService)
	 */
	@Override
	public void initingToWebApplication(BaseService<BaseModel> baseService)
	{
		
	}

}
